﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using Sebarf.Diagnostics.Interfaces;
using Utils;

#endregion

namespace paravili.Steps {
	/// <summary>
	/// Description of the class
	/// </summary>
	/// <summary>
	/// is a <see cref="ProcessStep{TSource}"/>
	/// converts the object in the source to the target type
	/// calls NextConvertedStep after process
	/// </summary>
	public abstract class ProcessStepConverterWithMeasurement<TSource, TTarget> : ProcessStepWithMeasurement<TSource> {
		#region Public Properties

		public new ProcessStepWithMeasurement<TTarget> NextStep { get; set; }

		#endregion

		#region Public Methods

		public override Object Process(TSource toProcess) {
			Check.AssertException(base.NextStep == null,
								   "provide the next step as ProcessStepWithMeasurement" + typeof(TTarget) + ">");
#if DEBUG
			if (IsEnableTimeMeasurement && StatisticValuesProviderService != null) {
				PerformanceTimer.Start();
			}
#endif
			TTarget convertedResult = Convert(toProcess);
#if DEBUG
			if (IsEnableTimeMeasurement && StatisticValuesProviderService != null) {
				PerformanceTimer.Stop();
				double timeNeeded = PerformanceTimer.ElapsedTimeMilliseconds.Miliseconds;
				StatisticValuesProviderService.SetStatisticValue(TimeMeasurementResultId, (int)timeNeeded);
			}
#endif
			if (NextStep == null) {
				return convertedResult;
			}
			else {
				return NextStep.Process(convertedResult);
			}
		}

		public override IProcessStep GetNextStep() {
			return NextStep;
		}

		protected override sealed TSource OnProcess(TSource toProcess) {
			throw new NotImplementedException();
		}

		#endregion

		#region Private Methods

		protected abstract TTarget Convert(TSource toProcess);

		#endregion
	}
}